/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package defaultPackage;

import entityBeans.ReplyEntity;
import entityBeans.SubjectEntity;
import entityBeans.ThreadEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import sessionBeans.SubjectSessionBeanLocal;
import sessionBeans.ThreadSessionBeanLocal;
import utilities.Constants;

/**
 *
 * @author h08125
 */
@ManagedBean(name = "subjectThread")
//Why @RequestScoped?
@SessionScoped
public class SubjectThreadManagedBean {
    @EJB
    private ThreadSessionBeanLocal threadSBL;
    @EJB
    private SubjectSessionBeanLocal subjectSBL;

    private SubjectEntity currentSubject;
    private ThreadEntity currentThread;
    
    private SubjectEntity newSubject;
    private ReplyEntity newReply;
    private ThreadEntity newThread;
    
    private String currentSubjectUuid;
    private String currentThreadUuid;
    public String basePath;
    public String baseModeratorPath;
    public int incr;
    public List<SubjectEntity> subjectList;
    
    
    /**
     * Creates a new instance of SubjectManagedBean
     */
    public SubjectThreadManagedBean() {
        this.currentSubjectUuid = null;
        this.currentSubject = null;
        this.currentThread = null;
        this.newSubject = new SubjectEntity();
        this.newThread = new ThreadEntity();
        this.newReply = new ReplyEntity();
        this.basePath = Constants.BASE_PATH;
        this.baseModeratorPath = Constants.BASE_MODERATOR_PATH;
        this.subjectList =(List) new ArrayList<SubjectEntity>();
    }

    public List<SubjectEntity> getSubjectList() {
        return subjectList;
    }

    public void setSubjectList(List<SubjectEntity> subjectList) {
        this.subjectList = subjectList;
    }

    public String getCurrentThreadUuid() {
        return currentThreadUuid;
    }

    public void setCurrentThreadUuid(String currentThreadUuid) {
        this.currentThreadUuid = currentThreadUuid;
    }
    
    public String getBasePath() {
        return basePath;
    }
    public String getBaseModeratorPath() {
        return baseModeratorPath;
    }
    public ReplyEntity getNewReply() {
        return newReply;
    }

    public void setNewReply(ReplyEntity newReply) {
        this.newReply = newReply;
    }

    public SubjectSessionBeanLocal getSubjectSBL() {
        return subjectSBL;
    }

    public void setSubjectSBL(SubjectSessionBeanLocal subjectSBL) {
        this.subjectSBL = subjectSBL;
    }
    public SubjectEntity getNewSubject() {
        return newSubject;
    }

    public void setNewSubject(SubjectEntity newSubject) {
        this.newSubject = newSubject;
    }

    public ThreadEntity getNewThread() {
        return newThread;
    }

    public void setNewThread(ThreadEntity newThread) {
        this.newThread = newThread;
    }

    public String getCurrentSubjectUuid() {
        return currentSubjectUuid;
    }

    public void setCurrentSubjectUuid(String currentSubjectUuid) {
        this.currentSubjectUuid = currentSubjectUuid;
    }

    public ThreadSessionBeanLocal getThreadSBL() {
        return threadSBL;
    }

    public void setThreadSBL(ThreadSessionBeanLocal threadSBL) {
        this.threadSBL = threadSBL;
    }

    public ThreadEntity getCurrentThread() {
        return currentThread;
    }

    public void setCurrentThread(ThreadEntity currentThread) {
        this.currentThread = currentThread;
    }

    public SubjectEntity getCurrentSubject() {
        return currentSubject;
    }

    public void setCurrentSubject(SubjectEntity currentSubject) {
        this.currentSubject = currentSubject;
    }



    /**
     * process the get http variable in the URL
     * Initialize the currentSubject object to the right value (depending on the get http variable)
     */
    public void initSubjectsList() {
        System.out.println("Uuid:"+currentSubjectUuid);
        // if id < 0, then we display the top level subjects
        currentSubject = (currentSubjectUuid != null) ? subjectSBL.getSubjectByUuid(currentSubjectUuid): subjectSBL.getTopLevelSubject();
    }
    /**
     * initialization of newThread page
     */
    public void initNewThread() {
        currentSubject = subjectSBL.getSubjectByUuid(currentSubjectUuid);
    }
    
    /**
     * populate subjectList to use it after
     */
    public void initializeAdminSubjectList() {
        resetNewEntities();
        subjectList.clear();  // initialize or reset the value if it is a reload
        currentSubject = subjectSBL.getTopLevelSubject();
        subjectList = subjectSBL.getAdminRecursiveOrder(currentSubject,(ArrayList) subjectList);
        // we need to populate the father as it is null by default
        newSubject.setMyFather(new SubjectEntity());
        // in addition, we absolutely need to set a father by default, top level subect then
        newSubject.getMyFather().setUuid(currentSubject.getUuid());
    }
    
    /**
     * initialization of the thread page
     */
    public void initThread() {
        currentThread = (currentThreadUuid != null) ? threadSBL.getThreadByUuid(currentThreadUuid) : null;
    }
    
    /**
     * Add a new subject with the currentSubject as father
     * @return the page to go to (have a look at face-config.xml)
     */
    public String addSubSubject() {
        if (!newSubject.getTitle().isEmpty()) {
            subjectSBL.addSubject(currentSubject,newSubject);
            resetNewEntities();
        }
        return "reload";
    }
    /**
     * Add a new subject with a father, but the father is only the uuid contained in the fatherSubject attribute
     * @return the page to go to (have a look at face-config.xml)
     */
    public String addNewSubject() {
        if (!newSubject.getTitle().isEmpty() && !newSubject.getMyFather().getUuid().isEmpty()) {
            subjectSBL.addSubject(null,newSubject);
        }
        return "reload";
    }
    
    /**
     * Add a new thread with the currentSubject as subject
     * @return the page to go to (have a look at face-config.xml)
     */
    public String addThread() {
        // if subject not null and title + content not empty
        if (currentSubject != null && !newThread.getTitle().isEmpty() && !newReply.getContent().isEmpty()) {
            newThread = threadSBL.addThread(currentSubject, newThread);
            threadSBL.addReply(newThread, newReply);
        }
        resetNewEntities();
        return "threadAdded";
    }
    /**
     * Extract from the DB the thread from a specific subject
     * @return List of thread depending on the subject
     */
    public List<ThreadEntity> getThreadBySubject() {
        return threadSBL.getThreadBySubject(currentSubject);
    }
    /**
     * Modify the given subject
     * @param mySubject subject to modify, with modified fields
     * @return the page to go to (have a look at face-config.xml)
     */
    public String modifySubject(SubjectEntity mySubject) {
        subjectSBL.modifySubject(mySubject);
        return "reload";
    }
    
    /**
     * Transfers the thread into currentThread then go to the return page
     * @param thread
     * @return  the page to go to (have a look at face-config.xml)
     */
    public String goToThread(ThreadEntity thread) {
        currentThread = thread;
        return "goToThread";
    }
    /**
     * Reset the different entities starting by "new"
     * To prevent some collision when adding several entities in a row
     */
    public void resetNewEntities() {
        newThread = new ThreadEntity();
        newSubject = new SubjectEntity();
        newReply = new ReplyEntity();
    }
}
